package com.purong.purong.activity;

import android.os.Handler;
import android.os.Message;
import android.text.Editable;
import android.text.TextWatcher;
import android.view.View;
import android.widget.Button;
import android.widget.EditText;
import android.widget.ImageView;
import android.widget.TextView;

import com.purong.purong.Contant;
import com.purong.purong.R;
import com.purong.purong.util.CommonUtil;
import com.yao.sdk.net.ResponseInfo;

import java.util.LinkedHashMap;
import java.util.Map;
import java.util.Timer;
import java.util.TimerTask;

/**
 * Created by wenjiezeng on 2017/10/5.
 */
public class ForgetPwdActivity extends BaseActivity {
    private EditText phoneEd,pwdEd, verEd;
    private TextView getCode;
    private View pwdLine,phoneLine,verLine;
    private ImageView phoneClear,pwdClear,verClear;
    private Button confirmBtn;
    private boolean phoneInput,pwdInput, verInput;
    private boolean canGetValid=true;
    private int timeCount=-1;


    @Override
    public int getRootViewId() {
        return R.layout.activity_forget_pwd;
    }

    private void getCode(String phone){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("phone",phone);
        canGetValid=!request(Contant.SMS, params, true,true);
    }
    private void findPwd(String phone, String pwd, String verification){
        Map<String, String> params = new LinkedHashMap<String, String>();
        params.put("user_phone",phone);
        params.put("new_pwd",pwd);
        params.put("verification",verification);
        request(Contant.FIND_PWD, params, true,true);
    }
    public void initView(){
        phoneLine=(View)findViewById(R.id.phone_cut_line);
        pwdLine=(View)findViewById(R.id.pwd_cut_line);
        verLine=(View)findViewById(R.id.ver_cut_line);
        getCode=(TextView)findViewById(R.id.get_code);
        phoneEd=(EditText)findViewById(R.id.phone_ed);
        pwdEd=(EditText)findViewById(R.id.pwd_ed);
        verEd =(EditText)findViewById(R.id.verification_ed);
        phoneClear=(ImageView)findViewById(R.id.phone_clear);
        pwdClear=(ImageView)findViewById(R.id.pwd_clear);
        verClear=(ImageView)findViewById(R.id.ver_clear);
        confirmBtn =(Button)findViewById(R.id.confirm) ;
        phoneEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    phoneInput = false;
                    phoneClear.setVisibility(View.INVISIBLE);
                } else {
                    phoneInput = true;
                    phoneClear.setVisibility(View.VISIBLE);
                }
                if (phoneInput && pwdInput && verInput) {
                    confirmBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirmBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        pwdEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    pwdInput = false;
                    pwdClear.setVisibility(View.GONE);
                } else {
                    pwdClear.setVisibility(View.VISIBLE);
                    if (s.toString().length()>=6){
                        pwdInput = true;
                    }else {
                        pwdInput = false;
                    }

                }
                if (phoneInput && pwdInput && verInput) {
                    confirmBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirmBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        verEd.addTextChangedListener(new TextWatcher() {
            @Override
            public void beforeTextChanged(CharSequence s, int start, int count, int after) {

            }

            @Override
            public void onTextChanged(CharSequence s, int start, int before, int count) {
                if (s.toString().trim().equals("")) {
                    verInput = false;
                    verClear.setVisibility(View.GONE);
                } else {
                    verInput = true;
                    verClear.setVisibility(View.VISIBLE);

                }
                if (phoneInput && pwdInput && verInput) {
                    confirmBtn.setBackgroundResource(R.drawable.button_blue_radius5);
                } else {
                    confirmBtn.setBackgroundResource(R.drawable.button_gray_radius5);

                }
            }

            @Override
            public void afterTextChanged(Editable s) {

            }
        });
        phoneEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    phoneLine.setBackgroundColor(getResources().getColor(R.color.blue));
                } else {
                    phoneLine.setBackgroundColor(getResources().getColor(R.color.cut_line));
                }
            }
        });
        pwdEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    pwdLine.setBackgroundColor(getResources().getColor(R.color.blue));
                } else {
                    pwdLine.setBackgroundColor(getResources().getColor(R.color.cut_line));

                }
            }
        });
        verEd.setOnFocusChangeListener(new View.OnFocusChangeListener() {
            @Override
            public void onFocusChange(View v, boolean hasFocus) {
                if (hasFocus) {
                    verLine.setBackgroundColor(getResources().getColor(R.color.blue));
                } else {
                    verLine.setBackgroundColor(getResources().getColor(R.color.cut_line));

                }
            }
        });
    }


    @Override
    public void initData() {

    }
    public void onClick(View v){
        switch (v.getId()){
            case R.id.left_back:
                finish();
                break;
            case R.id.get_code:
                if (!canGetValid)
                    return;
                if (!phoneInput) {
                    showToast("请输入手机号");
                    return;
                }
                if (!CommonUtil.isPhoneNum(phoneEd.getText().toString())) {
                    showToast("请输入正确的手机号");
                    return;
                }
                getCode(phoneEd.getText().toString());
                break;
            case R.id.confirm:
                if (!phoneInput) {
                    showToast("请输入手机号");
                    return;
                }
                if (!CommonUtil.isPhoneNum(phoneEd.getText().toString())) {
                    showToast("请输入正确的手机号");
                    return;
                }

                if (!verInput) {
                    showToast("请输入验证码");
                    return;
                }
                if (!pwdInput) {
                    showToast("请输入6-20位密码");
                    return;
                }
                findPwd(phoneEd.getText().toString(),pwdEd.getText().toString(), verEd.getText().toString());
                break;
            case R.id.phone_clear:
                phoneEd.setText("");
                break;
            case R.id.pwd_clear:
                pwdEd.setText("");
                break;
            case R.id.ver_clear:
                verEd.setText("");
                break;
        }
    }
    @Override
    public void onResponse(ResponseInfo responseInfo) {
        if (hasDestroy())
            return;
        super.onResponse(responseInfo);
        if (responseInfo.url.equals(Contant.SMS)){
            if (responseInfo.isRequestOk){
                showToast("验证码已发送");
                countDown();
            }else {
                canGetValid=true;
            }
        }else if (responseInfo.url.equals(Contant.FIND_PWD)){
            if (responseInfo.isRequestOk){
                showToast("修改成功");
                this.finish();
            }
        }
    }
    public void countDown(){
        timeCount=60;
        getCode.setTextColor(getResources().getColor(R.color.text_gray));
        Timer timer=new Timer();
        timer.schedule(new TimerTask() {
            @Override
            public void run() {
                timeCount--;
                handler.sendEmptyMessage(timeCount);
                if (timeCount==-1){
                    this.cancel();
                }
            }
        },0,1000);
    }
    Handler handler=new Handler(){
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (msg.what==-1){
                getCode.setTextColor(getResources().getColor(R.color.blue));
                getCode.setText("获取验证码");
                canGetValid=true;
            }else {
                getCode.setText("重新获取"+"("+msg.what+")秒");
            }
        }
    };

}
